package com.yycx.module.bus.provider.handler.invoice;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.bus.client.entity.*;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 开票税金分页列表扩展器
 *
 * @author zyf
 */
@Component("invoicePaymentPageListHandler")
public class InvoicePaymentPageListHandler implements PageInterceptor<BusPayment> {

    @Autowired
    private TaskService taskService;
    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        Integer paymentState=params.getInt("paymentState");
        String invoiceunitName=params.get("invoiceunitName");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String orderNo=params.get("orderNo");
        cq.select(BusPayment.class, "*");
        cq.select(BusSupplier.class, "supplierName");
        cq.select(BusInvoiceunitBank.class, "bankName");
        cq.select(BusInvoiceunit.class, "invoiceunitName");
        cq.addSelect(BaseUser.class, "nickName");
        cq.eq(BusPayment.class, "paymentId");
        cq.eq("payment.paymentNo",orderNo);
        cq.like("invoiceunit.invoiceunitName",invoiceunitName);
        cq.eq(true,"payment.companyId", OpenHelper.getCompanyId());
        if(paymentState.equals(0)){
            cq.eq("payment.paymentState", 0);
        }
        if(paymentState.equals(1)){
            cq.in("payment.paymentState", 0,1);
        }
        if(paymentState.equals(2)){
            cq.eq("payment.paymentState", 2);
        }
        cq.eq(BusPayment.class, "supplierId");
        cq.eq(BusPayment.class, "organizationId");
        cq.eq(true, "amountType", "Invoice");
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge(BusPayment.class, "payDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            cq.le(BusPayment.class, "payDate", endDate);
        }
        cq.createJoin(BusSupplier.class);
        cq.createJoin(BusInvoiceunit.class);
        cq.createJoin(BusInvoiceunitBank.class);
        cq.createJoin(BaseUser.class);
        cq.orderByDesc("payment.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }
}
